package com.gzhryc.net_marketing.cloud.micro.api.controllers.callback;

import com.gzhryc.common.logging.Logger;
import com.gzhryc.net_marketing.cloud.account.services.CloudPayAccountDepositOrderService;
import com.gzhryc.pay.adaptation.alipay.msg.native_pay.AlipayNativePayNotify;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.common.payment.dao.db.AlipayConfig;
import com.gzhryc.shared_device.common.payment.services.AlipayConfigService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

public class AlipayNativeCallbackController {

    static Logger log = Logger.getLogger(AlipayNativeCallbackController.class);

    @Route(value = "/paycall/alipay/{appId}/native/pay")
    public void payResult(HttpServletRequest request, HttpServletResponse response) {
        Map<String, String> params = WebUtils.getSingleParameter(request);
        log.info("支付宝Native支付回调参数：{{0:json}}", params);
        String appId = WebUtils.getString(request, "appId");
        try {
            AlipayConfig config = AlipayConfigService.self().getByAppId(appId);
            if(config != null) {
                AlipayNativePayNotify notify = new AlipayNativePayNotify(params, config.getAlipayPublicKey());
                CloudPayAccountDepositOrderService.self().payResult(notify);
            }else{
                log.error("未获取支付宝配置，应用ID：{{0}}",appId);
            }
        } catch (Exception e) {
            log.error(appId + "微信Native充值回调错误，" + e.getMessage(), e);
        }
        WebUtils.exportText(response, "success");
    }
}
